package tests;

import static org.junit.Assert.*;
import java.sql.Timestamp;
import java.util.Date;
import model.Email;
import model.EmailBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
 * This class tests the EmailBuilder class
 * 
 * @author buthainahaldosary
 * @since May 2013
 *
 * @version $Revision: 1.0 $
 */

public class TestEmailBuilder {
	
	 EmailBuilder emBuilder,result,emb;
	 Email em;
	 Date date;
	 Timestamp currentTime;
	 
	 /**
		 * This method is called before every test case to setup a new email builder, date and current time timestamp.
		 */
	@Before
	public void setUp()
	{
		emBuilder=new EmailBuilder();
		date= new Date();
		currentTime=(new Timestamp(date.getTime()));
	}
	
	/**
	 * This method is called after every test case to tear down the EmailBuilder and force garbage collection.
	 */
	@After
	public void tearDown()
	{
		emBuilder=null;
		result=null;
		System.gc();
		
		
	}
	
	/**
	 * This test case tests the setTo() method by setting a new to field and ensuring that the email To field equals the new To field.
	 */
	@Test
	public void testSetTo() {
		result=emBuilder.setTo("You@gmail.com");
		assertEquals("You@gmail.com",result.getTo());
	}

	/**
	 * This test case tests the setFrom() method by setting a new from field and ensuring that the email From field equals the new From field.
	 */
	@Test
	public void testSetFrom() {
		result=emBuilder.setFrom("Me@gmail.com");
		assertEquals("Me@gmail.com",result.getFrom());
	}

	/**
	 * This test case tests the setSubject() method by setting a new subject field and ensuring that the email Subject field equals the new Subject field.
	 */
	@Test
	public void testSetSubject() {
		result=emBuilder.setSubject("Hello!");
		assertEquals("Hello!",result.getSubject());
	}

	/**
	 * This test case tests the setBody() method by setting a new Body field and ensuring that the email Body field equals the new Body field.
	 */
	@Test
	public void testSetBody() {
		result=emBuilder.setBody("Just dropping by to say hello!");
		assertEquals("Just dropping by to say hello!",result.getBody());
	}

	/**
	 * This test case tests the setRead() method by setting a new Read field and ensuring that the email Read field equals the new Read field.
	 */
	@Test
	public void testSetRead() {
		result=emBuilder.setRead(false);
		assertEquals(false,result.isRead());
	}

	/**
	 * This test case tests the setTimestamp() method by setting a new Timestamp field and ensuring that the email Timestamp field equals the new Timestamp field.
	 */
	@Test
	public void testSetTimestamp() {
		
		result=emBuilder.setTimestamp(currentTime);
		assertEquals(currentTime,result.getTimestamp());
	}

	/**
	 * This test case tests the setFolderId() method by setting a new folder id field and ensuring that the email folder id field equals the new folder id field.
	 */
	@Test
	public void testSetFolderId() {
		result=emBuilder.setFolderId(1);
		assertEquals(1,result.getFolderId());
	}
	
	/**
	 * This test case tests the setId() method by setting a new Id field and ensuring that the email Id field equals the new Id field.
	 */
	@Test
	public void testSetId()
	{
		result=emBuilder.setId(5);
		assertEquals(5,result.getId());
	}

	/**
	 * This test case tests the Build method by initializing a new Email and checking it's fields against the specified values.
	 */
	@Test
	public void testBuild() {
		emb=new EmailBuilder();
		 em=emb.setTo("Sara@gmail.com").setFrom("Buthainah.aldosary@gmail.com").setSubject("Hello").setBody("Just dropping by to say hello")
				 .setRead(false).setTimestamp(currentTime).setFolderId(1).build();
		 assertEquals("Sara@gmail.com",em.getTo());
		 assertEquals("Buthainah.aldosary@gmail.com",em.getFrom());
		 assertEquals("Hello",em.getSubject());
		 assertEquals("Just dropping by to say hello",em.getBody());
		 assertEquals(false,em.isRead());
		 assertEquals(currentTime,em.getTimestamp());
		 assertEquals(1,em.getFolderId());
	}

}
